package com.gpnu.crm.web.controller;
import com.gpnu.crm.domain.CustomerTransfer;
import com.gpnu.crm.query.ChartQueryObject;
import com.gpnu.crm.query.CustomerTransferQueryObject;
import com.gpnu.crm.service.IChartService;
import com.gpnu.crm.util.JSONUtil;
import com.gpnu.crm.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("chart")
public class ChartController {
    @Autowired
    private IChartService chartService;

    @RequestMapping("list")

    public String list(@ModelAttribute("qo") ChartQueryObject qo, Model model) {
        //查询报表数据，共享给页面做显示
    	model.addAttribute("list", chartService.queryChart(qo));
        return "chart/list";
    }

    @RequestMapping("queryBar")
    public String queryBar(@ModelAttribute("qo") ChartQueryObject qo, Model model) {
    	List<Map<String, Object>> list = chartService.queryChart(qo);
    	//${groupType}
    	model.addAttribute("groupType", MessageUtil.changMsg(qo));
    	List<Object> x = new ArrayList<>();
    	List<Object> y = new ArrayList<>();
    	for (Map<String, Object> map : list) {
			x.add(map.get("groupType")); //取出列名，就是X
			y.add(map.get("num")); //取出列对应在只，就是Y
		}
    	model.addAttribute("x", JSONUtil.toJSONString(x));
    	model.addAttribute("y", JSONUtil.toJSONString(y));
        return "chart/bar";
    }

    @RequestMapping("queryPie")
    public String queryPie(@ModelAttribute("qo") ChartQueryObject qo, Model model) {
    	List<Map<String, Object>> list = chartService.queryChart(qo);
    	//${groupType}
    	model.addAttribute("groupType", MessageUtil.changMsg(qo));
    	List<Object> legendDate = new ArrayList<>();
    	List<Object> seriesDate = new ArrayList<>();
    	for (Map<String, Object> map : list) {
    		Object groupType = map.get("groupType");
			legendDate.add(groupType); //取出列名，就是X
			//封装饼图在面积
			Map<String, Object> data = new HashMap<>();
			data.put("name", groupType);
			data.put("value", map.get("num"));
			seriesDate.add(data); //封装饼图在面积
		}
    	model.addAttribute("legendDate", JSONUtil.toJSONString(legendDate));
    	model.addAttribute("seriesDate", JSONUtil.toJSONString(seriesDate));
        return "chart/pie";
    }
}